from django.shortcuts import render
from django.http import JsonResponse
from misapp.models import User
from misapp.models import Drug
from misapp.models import saleSlip
from misapp.models import purchaseOrder
from misapp.models import sale
from misapp.models import purchase
import json


# Create your views here.
def index(request):
    return render(request, 'D:/desktop/drugManagement/frontend/dist/index.html')


def register(request):
    response = {}
    try:
        print(request.GET.get('username'), request.GET.get('password'), request.GET.get('id'))
        user = User(username=request.GET.get('username'), password=request.GET.get('password'),
                    identify=request.GET.get('id'))
        user.save()
        response['msg'] = 'success'
        response['error_num'] = 0
    except Exception as e:
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)


def login(request):
    response = {}
    try:
        user = User.objects.filter(username=request.GET.get('username')).first()
        print(user.password)
        print(111)
        psw = request.GET.get('password')
        print(psw)
        if user.password == psw:
            response['msg'] = 'success'
            response['error_num'] = 0
            response['id'] = user.identify
        else:
            response['msg'] = 'wrong password'
            response['error_num'] = 0
    except Exception as e:
        response['msg'] = str(e)
        response['error_num'] = 1
        response['id'] = -1
    return JsonResponse(response)

def getUserInfo(request):
    response = {}
    try:
        print(request.GET.get('username'))
        user = User.objects.filter(username=request.GET.get('username')).first()
        response['msg'] = 'success'
        response['username'] = user.username
        response['id'] = user.identify
        print(user.username, user.identify)
    except Exception as e:
        response['msg'] = str(e)
        response['error_num'] = 2
    return JsonResponse(response)

def createPurchaseOrder(request):
    response = {}
    try:
        dno = request.GET.get('dno')
        num = request.GET.get('num')

        drug = Drug.object.filter(dno=dno).first()
        drug.dNum = drug.dnum - num
        drug.dNum.save()
        pid = purchaseOrder.object.filter().count
        newOrder = purchaseOrder(pid)
        newOrder.save()
        newPurchase = purchase(pid=pid, dno=dno, num=num)
        newPurchase.save()
        response['msg'] = 'success'
        response['error_num'] = 0

    except Exception as e:
        response['msg'] = str(e)
        response['error_num'] = 3
    return JsonResponse(response)

def createSaleSlip(request):
    response = {}
    try:
        dno = request.GET.get('dno')
        num = request.GET.get('num')
        drug = Drug.object.filter(dno=dno).first()
        if num > drug.dNum :
            response['msg'] = 'too large amount'
            response['error_num'] = 0;
        else:
            drug.dNum = drug.dNum - num
            drug.save()
            sid = sale.object.filter().count;
            newOrder = saleSlip(sid)
            newOrder.save()
            newSale = sale(sid=sid, dno=dno, num=num)
            newSale.save()
            response['msg'] = 'success'
            response['error_num'] = 0

    except Exception as e:
        response['msg'] = str(e)
        response['error_num'] = 3
    return JsonResponse(response)

def getDrugInfo(request):
    response = {}
    try:
        if request.GET.get('condition') == '':
            drugs = Drug.object.filter()

    except Exception as e:
        a = 1
    return 0